package com.woniu.service.impl;

import com.woniu.dao.AuthDao;
import com.woniu.entity.Order;
import com.woniu.entity.User;
import com.woniu.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    AuthDao authDao;

    @Override
    @Cacheable(value = "user",key = "#user.getUsername()")
    public User findUser(User user) {
        return authDao.findByUsername(user.getUsername());
    }

    @Override
    @CachePut(value = "user",key = "#user.getUsername()")
    public Object updateUser(User user) {
        return authDao.save(user);
    }

    @Override
    //allEntries为true就会清除所有的user::* 的缓存记录 key = "admin" 则无效
    @CacheEvict(value = "user",key = "admin",allEntries = true,beforeInvocation = true)
    public void clearCache() {
        int i = 1/0;//beforeInvocation设置为true，即使里面报错 也能清除所有缓存
    }
}
